package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.OrganActivities;
/**
 * 公司活动
 */
@Service
public class OrganActivitiesDAO extends BaseDAO<OrganActivities, Serializable>{
	private static final String sql_indexlist = "SELECT oa.code, oa.title FROM OrganActivities oa WHERE oa.deleted = 0 AND oa.oc = ? " +
			"AND oa.type = ? ORDER BY oa.topTime DESC LIMIT 5";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM OrganActivities oa WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String title = map.get(Parm.TITLE);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String type = map.get(Parm.TYPE);
			String deleted = map.get(Parm.DELETED);
			String oc = map.get(Parm.OC);
			if (oc != "" && isNotNull(oc)) {
				hql.append(" AND oa.oc = ? ");
				list.add(Integer.parseInt(oc));
			}
			if (title != "" && isNotNull(title)) {
				hql.append(" AND oa.title LIKE ? ");
				list.add("%" + title + "%");
			}
			if (type != "" && isNotNull(type)) {
				hql.append(" AND oa.type = ? ");
				list.add(Byte.parseByte(type));
			}
			if(time1!=""&&isNotNull(time1)){
				hql.append(" AND oa.ct >= ? ");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}
			if(time2!=""&&isNotNull(time2)){
				hql.append(" AND oa.ct <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
			if(deleted!=""&&isNotNull(deleted)){
				hql.append(" AND oa.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY oa.topTime DESC");
		return findToPageBean(hql.toString(), pageBean, list);
	}
	
	//获取首页List
	public List<OrganActivities> getIndexList(Integer code, String type){
		return query(sql_indexlist,null,null,code,Byte.parseByte(type));
	}
}
